<?php

namespace Admin\Controller;

use Think\Controller;

class PublicController extends Controller {

    //用户登陆
    public function login() {
        if (IS_POST) {
            $username = I("username");
            $password = I("password");
            $uid = D("UcenterMember")->login($username, $password);

            if ($uid > 0) {
                if (!is_administrator($uid)) {
                    $rule = strtolower('admin/index/index');
                    if (!$Auth) {
                        $Auth = new \Think\Auth();
                    }
                    if (!$Auth->check($rule, $uid, array('in', '1,2'), 'url')) {
                        $this->error("没有登陆权限");
                    }
                }
                if (D("Member")->login($uid)) {
                    $this->success("登陆成功", U('Index/index'));
                } else {
                    $this->error(D("Member")->getError());
                }
            } else {

                switch ($uid) {
                    case -1: $error = '用户不存在或被禁用！';
                        break; //系统级别禁用
                    case -2: $error = '密码错误！';
                        break;
                    default: $error = '未知错误！';
                        break; // 0-接口参数错误（调试阶段使用）
                }
                $this->error($error);
            }
        }
        $this->display();
    }

    public function logout() {
        if (is_login()) {
            D('Member')->logout();
            $this->success('退出成功！', U('login'), false);
        } else {
            $this->redirect('login');
        }
    }

    public function regUser() {
        $this->display();
    }

}
